/**
 * 工具相关的TypeScript类型定义
 * 从ai-writer-lite项目迁移
 */

import { BaseMessage } from "@langchain/core/messages";

// 基础工具接口
export interface BaseTool {
  name: string;
  description: string;
  category: ToolCategory;
  parameters: ToolParameter[];
  execute: (input: any) => Promise<ToolResult>;
  enabled: boolean;
  requiresAuth?: boolean;
  config?: Record<string, any>;
}

// 工具分类
export type ToolCategory =
  | 'textGeneration'
  | 'textAnalysis'
  | 'memoryManagement'
  | 'projectManagement'
  | 'research'
  | 'multimodal'
  | 'collaboration'
  | 'optimization';

// 工具参数定义
export interface ToolParameter {
  name: string;
  type: 'string' | 'number' | 'boolean' | 'array' | 'object';
  description: string;
  required: boolean;
  default?: any;
  validation?: ParameterValidation;
}

// 参数验证规则
export interface ParameterValidation {
  min?: number;
  max?: number;
  pattern?: string;
  enum?: any[];
  custom?: (value: any) => boolean | string;
}

// 工具执行结果
export interface ToolResult {
  success: boolean;
  data?: any;
  error?: string;
  metadata?: ToolResultMetadata;
  executionTime?: number;
}

// 工具结果元数据
export interface ToolResultMetadata {
  tokensUsed?: number;
  cost?: number;
  model?: string;
  provider?: string;
  cacheHit?: boolean;
  confidence?: number;
  sources?: string[];
}

// 文本生成工具参数
export interface TextGenerationParams {
  prompt: string;
  context?: string;
  style?: 'formal' | 'casual' | 'academic' | 'creative' | 'technical';
  tone?: 'neutral' | 'positive' | 'negative' | 'persuasive';
  length?: 'short' | 'medium' | 'long';
  language?: string;
  temperature?: number;
  maxTokens?: number;
}

// 文本分析工具参数
export interface TextAnalysisParams {
  text: string;
  analysisType: AnalysisType;
  options?: AnalysisOptions;
}

// 分析类型
export type AnalysisType =
  | 'sentiment'
  | 'readability'
  | 'style'
  | 'structure'
  | 'grammar'
  | 'plagiarism'
  | 'seo'
  | 'keywords'
  | 'summarization';

// 分析选项
export interface AnalysisOptions {
  detailLevel: 'basic' | 'detailed' | 'comprehensive';
  includeSuggestions?: boolean;
  targetAudience?: string;
  language?: string;
}

// 内存管理工具参数
export interface MemoryManagementParams {
  action: MemoryAction;
  data?: any;
  query?: string;
  options?: MemoryOptions;
}

// 内存操作类型
export type MemoryAction =
  | 'store'
  | 'retrieve'
  | 'search'
  | 'update'
  | 'delete'
  | 'clear';

// 内存操作选项
export interface MemoryOptions {
  memoryType: 'conversational' | 'vector' | 'episodic' | 'knowledge';
  namespace?: string;
  limit?: number;
  threshold?: number;
}

// 项目管理工具参数
export interface ProjectManagementParams {
  action: ProjectAction;
  projectId?: string;
  chapterId?: string;
  data?: any;
}

// 项目操作类型
export type ProjectAction =
  | 'create'
  | 'read'
  | 'update'
  | 'delete'
  | 'list'
  | 'search'
  | 'backup'
  | 'restore';

// 研究工具参数
export interface ResearchParams {
  query: string;
  sources?: ResearchSource[];
  depth?: 'shallow' | 'medium' | 'deep';
  maxResults?: number;
  language?: string;
}

// 研究来源类型
export type ResearchSource =
  | 'web'
  | 'academic'
  | 'news'
  | 'books'
  | 'wikipedia'
  | 'custom';

// 多模态工具参数
export interface MultimodalParams {
  input: MultimodalInput;
  operation: MultimodalOperation;
  options?: MultimodalOptions;
}

// 多模态输入
export interface MultimodalInput {
  text?: string;
  image?: string | Buffer;
  audio?: string | Buffer;
  video?: string | Buffer;
}

// 多模态操作类型
export type MultimodalOperation =
  | 'analyze'
  | 'transcribe'
  | 'generate'
  | 'translate'
  | 'extract';

// 多模态选项
export interface MultimodalOptions {
  outputFormat?: 'text' | 'json' | 'structured';
  language?: string;
  detail?: 'low' | 'medium' | 'high';
}

// 协作工具参数
export interface CollaborationParams {
  action: CollaborationAction;
  projectId: string;
  userId?: string;
  data?: any;
}

// 协作操作类型
export type CollaborationAction =
  | 'invite'
  | 'join'
  | 'leave'
  | 'edit'
  | 'comment'
  | 'review'
  | 'merge'
  | 'conflict_resolve';

// 优化工具参数
export interface OptimizationParams {
  target: OptimizationTarget;
  text: string;
  goals?: OptimizationGoal[];
  constraints?: OptimizationConstraints;
}

// 优化目标
export type OptimizationTarget =
  | 'readability'
  | 'seo'
  | 'engagement'
  | 'clarity'
  | 'length'
  | 'tone'
  | 'style';

// 优化目标详情
export interface OptimizationGoal {
  type: OptimizationTarget;
  priority: number;
  targetValue?: number;
}

// 优化约束
export interface OptimizationConstraints {
  preserveMeaning: boolean;
  maxLength?: number;
  minLength?: number;
  maintainStyle?: boolean;
  avoidWords?: string[];
}

// 工具注册信息
export interface ToolRegistration {
  tool: BaseTool;
  version: string;
  dependencies: string[];
  permissions: string[];
  rateLimit?: RateLimit;
}

// 速率限制
export interface RateLimit {
  requestsPerMinute: number;
  requestsPerHour: number;
  requestsPerDay: number;
  tokensPerMinute: number;
}

// 工具执行状态
export interface ToolExecutionState {
  toolName: string;
  status: 'idle' | 'running' | 'completed' | 'error';
  startTime?: Date;
  endTime?: Date;
  progress?: number;
  currentStep?: string;
  error?: string;
}

// 工具统计信息
export interface ToolStatistics {
  totalExecutions: number;
  successfulExecutions: number;
  failedExecutions: number;
  averageExecutionTime: number;
  totalTokensUsed: number;
  totalCost: number;
  lastUsed?: Date;
  mostUsedWith: string[];
}

// 导出所有类型
export type {
  BaseTool,
  ToolCategory,
  ToolParameter,
  ParameterValidation,
  ToolResult,
  ToolResultMetadata,
  TextGenerationParams,
  TextAnalysisParams,
  AnalysisType,
  AnalysisOptions,
  MemoryManagementParams,
  MemoryAction,
  MemoryOptions,
  ProjectManagementParams,
  ProjectAction,
  ResearchParams,
  ResearchSource,
  MultimodalParams,
  MultimodalInput,
  MultimodalOperation,
  MultimodalOptions,
  CollaborationParams,
  CollaborationAction,
  OptimizationParams,
  OptimizationTarget,
  OptimizationGoal,
  OptimizationConstraints,
  ToolRegistration,
  RateLimit,
  ToolExecutionState,
  ToolStatistics,
};